import { ChannelType } from '@/store/reducers/channel';
import { AuthType } from '@/store/reducers/user';

const TOKEN_KEY = 'geek-mie-69';
// 获取token
export const getToken = (): AuthType => {
  return JSON.parse(localStorage.getItem(TOKEN_KEY) || '{}');
};
// 存储token
export const setToken = (tokenInfo: any): void => {
  localStorage.setItem(TOKEN_KEY, JSON.stringify(tokenInfo));
};

export const clearToken = (): void => {
  localStorage.removeItem(TOKEN_KEY);
};
export const hasToken = (): boolean => {
  return !!getToken().token;
};

// 封装频道缓存
const CHANNELS_KEY = 'channels-mie-69';
export const setLocalChannels = (channels: ChannelType[]) => {
  localStorage.setItem(CHANNELS_KEY, JSON.stringify(channels));
};
export const getLocalChannel = (): ChannelType[] => {
  return JSON.parse(localStorage.getItem(CHANNELS_KEY) || '[]');
};

export const removeLocalChannel = () => {
  localStorage.removeItem(CHANNELS_KEY);
};
// 封装历史
const HISTORIES_KEY = 'histories-mie-69';
export const setLocalHistories = (histories: string[]) => {
  localStorage.setItem(HISTORIES_KEY, JSON.stringify(histories));
};
export const getLocalHistories = (): string[] => {
  return JSON.parse(localStorage.getItem(HISTORIES_KEY) || '[]');
};

export const removeLocalHistories = () => {
  localStorage.removeItem(HISTORIES_KEY);
};
